package org.display.util
{
	import flash.filesystem.*;
	import flash.utils.*;
	
	import org.data.*;
	import org.events.*;
	import org.net.*;
	import org.utils.*;

	public class AddEvent
	{
		private var path:String=""
		private var classname:String=""
			
		private var str:String = ""
		private var arr:Array = []
			
		public function AddEvent(type:String,classname:String,path:String)
		{
			
			this.classname=classname
			
			this.path=path
				
			if(type=="添加异步事件"){
			
				asynEvent()
				
				modifyCommandControl("Asyn")
			
			}else if(type=="添加功能事件"){
			
				funEvent()
				
				modifyCommandControl("Fun")
			
			}else if(type=="添加关联事件"){
				
				relEvent()
				
				modifyCommandControl("Rel")
				
			}
			
			modifyCommand()
			
			

		}
		
		function modifyCommandControl(sub:String){
		
			var arr=path.replace(/.*?(?=\\org\\)/gis,"").split("\\")
			
			arr.shift()			
			arr[arr.length-1]=classname+sub

			var event:String=arr.join(".")
				
			arr.pop()
			arr.pop()
			
			var res:AbcResponder=new AbcResponder()
			
			res.onResult=function(e,path){
				
				var source:String=e
				
				var regstr:String="public function initializeController(){"
				
				var newstr:String=regstr+"\n       AbcController.instance.registerCommand(\""+event+"\");"
				
				source=source.replace(new RegExp(RegControl.transform(regstr),"gis"),newstr)
				
				setTimeout(FileControl.saveFile,100,path,source)
				
			}
			
				
			var p:Array=path.split("\\")
			p.pop()
			p.pop()
			
			new AbcHTTPServices(p.join("\\")+"\\CommandControl.as",res)

		}
		
		function modifyCommand(){
			
			var arr=path.replace(/.*?(?=\\org\\)/gis,"").split("\\")
			
			arr.shift()
			
			arr[arr.length-1]=classname
		
			var res:AbcResponder=new AbcResponder()
			
			res.onResult=function(e,path){
				
				var source:String=e
					
				var regstr:String="public override function execute(e:AbcEvent) : void{"
				
				var newstr:String=regstr+"\n       AbcEventDispatcher.instance.dispatcher(\""+arr.join(".")+"\",data);"
					
				source=source.replace(new RegExp(RegControl.transform(regstr),"gis"),newstr)
                
				setTimeout(FileControl.saveFile,100,path,source)
					
			}
			
			new AbcHTTPServices(path,res)
		
		}
		
	    private function asynEvent(){
		
			var arr:Array=path.split("\\")
				
			arr.pop()
			var commandPath:String=arr.join("\\")+"\\"+classname+"Asyn.as"
				
			
			if(File.desktopDirectory.resolvePath(commandPath).exists==false){
				
				FileControl.saveFile(commandPath,getCommandSource("Asyn"))
				
			}
		}
		
		private function funEvent(){

			var arr:Array=path.split("\\")
			arr.pop()
			var commandPath:String=arr.join("\\")+"\\"+classname+"Fun.as"
				

			
			if(File.desktopDirectory.resolvePath(commandPath).exists==false){
				
				FileControl.saveFile(commandPath,getCommandSource("Fun"))
				
			}

		}
		
		private function relEvent(){
			
			var arr:Array=path.split("\\")
			arr.pop()
			var commandPath:String=arr.join("\\")+"\\"+classname+"Rel.as"
			

			
			if(File.desktopDirectory.resolvePath(commandPath).exists==false){
				
				FileControl.saveFile(commandPath,getCommandSource("Rel"))
				
			}
			
		}
		
		function getCommandSource(suffix:String):String{

			var str:String = path.replace(/.*?(?=\\org\\)/gis,"")
			var arr:Array =str.split("\\")
			arr.shift()
			arr.pop()
				
			var commandSource:String=Global.commandSource
			commandSource=commandSource.replace(/<package>/gis,arr.join("."))
				
			commandSource=commandSource.replace(/<import>/gis,"import "+arr.join(".").replace(/\.commands\./gis,".display."))
			commandSource=commandSource.replace(/<class>/gis,classname+suffix)
			commandSource=commandSource.replace(/<type>/gis,arr[arr.length-1])
				
			return commandSource
				
		}
		
	}
}